import { Controller, Context } from "egg";
import ApplicationService from "../../service/application";

/**
 * @Controller 设备管理 DevicesController
 */
export default class ApplicationController extends Controller implements RestfulController {
  private _service: ApplicationService;

  constructor(ctx: Context) {
    super(ctx);
    this._service = ctx.service.device;
  }

  /**
   * @summary 创建设备
   * @description 创建设备
   * @router POST /api/v1/devices
   * @request body DeviceModel
   * @response 200 BaseResModel
   */
  async create() {
    const resp = await this._service.create(this.ctx.request.body);
    this.ctx.body = resp.body;
    this.ctx.status = resp.status;
  }

  /**
   * @summary 删除设备
   * @description 删除设备
   * @router DELETE /api/v1/devices/{id}
   * @request path string *id 设备id
   * @response 200 BaseResModel
   */
  async destroy() {
    const { ctx } = this;
    const resp = await this._service.destroy(ctx.params.id);
    ctx.body = resp.body;
    ctx.status = resp.status;
  }

  /**
   * @summary 修改设备
   * @description 修改设备
   * @router PUT /api/v1/devices/{id}
   * @request path string *id 设备id
   * @request body DeviceModel
   * @response 200 BaseResModel
   */
  async update() {
    const resp = await this._service.update(this.ctx.params.id, this.ctx.request.body);
    this.ctx.body = resp.body;
    this.ctx.status = resp.status;
  }

  /**
   * @summary 查询设备分页列表
   * @description 查询设备分页列表
   * @router GET /api/v1/devices
   * @request query string deviceName 设备名称
   * @request query string appId 应用Id
   * @response 200 BaseResModel
   */
  async index() {
    const { ctx } = this;
    const resp = await this._service.index(ctx.query);
    ctx.body = resp.body;
    ctx.status = resp.status;
  }

  /**
   * @summary 查询设备明细
   * @description 查询设备明细
   * @router GET /api/v1/devices/{id}
   * @request path string *id 设备id
   * @response 200 BaseResModel
   */
  async show() {
    const resp = await this._service.show(this.ctx.params.id);
    this.ctx.body = resp.body;
    this.ctx.status = resp.status;
  }

  /**
   * @summary 查询设备分页列表
   * @description 查询设备分页列表
   * @router GET /api/v1/page/devices
   * @request query integer *current 当前页
   * @request query integer *pageSize 每页条数
   * @request query string deviceName 设备名称
   * @request query string appId 应用Id
   * @response 200 BaseResModel
   */
  async page() {
    const resp = await this._service.page(this.ctx.query);
    this.ctx.body = resp.body;
    this.ctx.status = resp.status;
  }
}
